package com.cy.session;

import java.io.IOException;
import java.io.Serializable;
import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;




import sun.misc.BASE64Decoder;


public class SessionHelper implements Serializable {
	

	/**
	 * 设置后台登录人员Session
	 */
	public static void setBackUser(SessionUser sessionUser) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute(SessionUser.SESSION_BACKUSER_KEY, sessionUser);
	}

	/**
	 * 取得后台登录人员Session
	 */
	public static SessionUser getSessionUser() {
		HttpServletRequest request=null;
		HttpSession sess = null;
		if (ServletActionContext.getContext()!=null){	//action页面跳用
			request = ServletActionContext.getRequest();
		}		
		if (request != null) {
			sess = request.getSession();
		} else {	//DWR调用
			WebContext wc = WebContextFactory.get();
			if (wc != null) {
				sess = wc.getSession();
			}
		}
		if (sess == null) { return null; }
		SessionUser sa = (SessionUser) sess.getAttribute(SessionUser.SESSION_BACKUSER_KEY);
		return sa;
	}
	
	public static void removeSession(String cookieKey) {
		HttpSession session = (HttpSession) ServletActionContext.getRequest().getSession();
		if (session == null) { return; }
		session.removeAttribute(cookieKey);
		session.invalidate();
	}
}
